$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + '/notice/adminList',
		datatype: "json",
		colModel: [
			{ label: 'ID', name: 'id', width: 10, key: true },
			{ label: '标题', name: 'title',  key: true },
			{ label: '发布者', name: 'publisher', width: 20, key: true },
			{ label: '目标对象', name: 'level', width: 20, key: true,formatter: function(value, options, row){
				var str;
				switch(value){
                    case 0:
                        str= '单用户';
                        break;
                    case 1:
                        str= '试客';
                        break;
                    case 2:
                        str= '商户';
                        break;
                    case 3:
                        str= '所有用户';
                        break;
				}
				return str;
			} },
			{ label: '单用户', name: 'owner', width: 20, key: true },
			{ label: '操作',  width: 20, key: true, formatter: function(value, options, row){
                    return '<button class="btn btn-danger" onclick="del('+row.id+')">删除</button>';
                }},
		],
		viewrecords: true,
		height: 555,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true, 
		rownumWidth: 25, 
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "data.dataList",
			page: "data.pageNo",
			records: "data.totalNum"
		},
		prmNames : {
			page:"pageNo", 
			rows:"pageSize", 
			order: "order"
		},
		gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			key: null
		},
		showList: true,
		title: null,
		form:{
            title:null,
            content:null,
            level:3,
            owner:null

		},
		userList:null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add:function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "公告内容",
                area: ['550px', '600px'],
                shadeClose: false,
                contentType:"application/json",
                content: jQuery("#addNotice"),
                btn: ['发布','取消'],
                btn1: function (index) {
                	vm.form.content = document.getElementById('description').contentWindow.editor.html()
                    $.ajax({
                        type: "POST",
                        url: baseURL + "/notice/publish",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(vm.form),
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                layer.alert('发布成功', function(){
                                    location.reload();
                                });
                            }else{
                                layer.alert(r.info);
                            }
                        }
                    });
                }
            });
        }
	},
	mounted:function () {
		//获取所有用户
        $.ajax({
            type: "POST",
            url: baseURL + "/auth/adminUserList",
            dataType: "json",
            contentType:"application/json",
            success: function(r){
                if (r.code===0) {
                	console.log(r);
					vm.userList = r.data;
                }else{
                    alert(r.info);
                }

            }
        });
	}
});
function del(id) {
    confirm("您确定删除吗？",function () {
        var param ={
            'id':id
        }
        $.ajax({
            type: "POST",
            url: baseURL + "/notice/delete",
            dataType: "json",
            contentType:"application/json",
            data:JSON.stringify(param),
            success: function(r){
                if (r.code===0) {
                    alert(r.info,function () {
                        location.reload();
                    })
                }else{
                    alert(r.info);
                }

            }
        });
	})
}
